//
//  OptionsViewController.m
//  TodatPlay
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "OptionsViewController.h"
#import "OptionsTableViewCell.h"
#import "SDAutoLayout.h"
#import "CacheTableViewCell.h"
#import "CDMClearCache.h"
#import "SVProgressHUD.h"

#define kTableViewCellReuse @"reuse"
#define kTableViewCellCacheReuse @"cacheReuse"
#define filePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
@interface OptionsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *optionsTableView;
@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation OptionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(handleleft:)];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    self.navigationItem.titleView = label;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;

    label.text = @"设置";
    
    [self.optionsTableView registerNib:[UINib nibWithNibName:@"OptionsTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    [self.optionsTableView registerNib:[UINib nibWithNibName:@"CacheTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellCacheReuse];
    _titleArr = [NSArray array];
    [self handleData];
    
}
- (void)handleleft:(UIBarButtonItem *)barBtn {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)handleData {
    _titleArr = @[@"清除缓存", @"意见反馈", @"检查更新", @"商务合作", @"关于今天玩什么"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 5;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OptionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
        cell.titleLabel.text = @"推送设置";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else {
        if (indexPath.row == 0) {
            NSString *fileSize = [CDMClearCache getCacheSizeWithFilePath:filePath];
            CacheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellCacheReuse];
            cell.titleLabel.text = _titleArr[indexPath.row];
            if (fileSize.integerValue == 0) {
                cell.cachelabel.text = @"0M";
            } else {
            cell.cachelabel.text = fileSize;
            }
            
            return cell;
        } else {
            OptionsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
            cell.titleLabel.text = _titleArr[indexPath.row];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0&&indexPath.section==1) {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"确定清除缓存吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //创建一个取消和一个确定按钮
        UIAlertAction *actionCancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        //因为需要点击确定按钮后改变文字的值，所以需要在确定按钮这个block里面进行相应的操作
        UIAlertAction *actionOk=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            //清楚缓存
            BOOL isSuccess = [CDMClearCache clearCacheWithFilePath:filePath];
            if (isSuccess) {
                [self.optionsTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD showSuccessWithStatus:@"清除成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
            
            
        }   ];
        //将取消和确定按钮添加进弹框控制器
        [alert addAction:actionCancle];
        [alert addAction:actionOk];
        //添加一个文本框到弹框控制器
        
        //显示弹框控制器
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
